<?php

if (!defined('BASEPATH'))
	exit('No direct script access allowed');

/**
 * Description of system
 *
 * @author Admin
 */
class system extends MY_Controller {

	public function index() {
		$this->data['temp'] = 'admin/system/index';
		$this->load->view('admin/main', $this->data);
	}

	public function create() {
		$data = request_filter(array('name', 'controller', 'class', 'p_id', 'description', 'active', 'index'));
		$this->model_module->create($data);
		echo json_encode(array(
			'title' => lang('notice'),
			'state' => 'success',
			'message' => lang('create_success')
		));
	}

	public function read() {
		$modules = $this->model_module->get_list_hash(array(
			'order' => array('index', 'asc')
				));

		foreach ($modules as &$module)
			load_children($module, $modules);

		$data = array();
		object_prefix_create($modules, '', '&nbsp;&nbsp;&nbsp;&nbsp;');

		object_to_array($data, $modules);

		$result = array(
			'title' => lang('notice'),
			'state' => 'success',
			'data' => $data,
		);
		echo json_encode($result);
	}

	public function detail() {
		$id = request('id');
		$module = $this->model_module->get_info($id);
		if ($module === false)
			$result = array(
				'state' => 'error',
				'title' => lang('error'),
				'message' => lang('module_not_found')
			);
		else {
			// Load cac module co the lam module cha
			$parents = $this->data['menus'];
			delete_children($module, $parents);
			object_prefix_create($parents, '', '&nbsp;&nbsp;&nbsp;&nbsp;');
			$data = array();
			object_to_array($data, $parents);
			$module->parents = $data;
			$result = array(
				'title' => lang('notice'),
				'state' => 'success',
				'data' => $module
			);
		}
		echo json_encode($result);
	}

	public function update() {
		$id = request('id');
		$data = request_filter(array('name', 'controller', 'class', 'p_id', 'description', 'active', 'index'));
		$this->model_module->update($id, $data);
		echo json_encode(array(
			'id' => $id,
			'title' => lang('notice'),
			'state' => 'success',
			'message' => lang('module_update_success')
		));
	}

	public function delete() {
		$id = request('id');
		$children = $this->model_module->get_list(array(
			'where' => array('p_id' => $id)
				));
		if (count($children) > 0)
			$result = array(
				'title' => lang('error'),
				'state' => 'error',
				'message' => lang('module_delete_contain_child')
			);
		else {
			$this->model_module->del($id);
			$result = array(
				'id' => $id,
				'state' => 'success',
				'title' => lang('notice'),
				'message' => lang('module_delete_success')
			);
		}
		echo json_encode($result);
	}

	public function set_state() {
		$id = request('id');
		if ($id == 0) {
			echo json_encode(array(
				'title' => lang('error'),
				'state' => 'error',
				'message' => lang('module_cant_be_inactive')
			));
			return;
		}
		$data = request_filter(array('active'));
		$this->model_module->update($id, $data);
		echo json_encode(array(
			'id' => $id,
			'active' => $data['active'],
			'title' => lang('notice'),
			'state' => 'success',
			'message' => lang('module_update_success')
		));
	}

}

?>
